package com.itheima.pinda.impl.truck;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.entity.dto.PdTruckTypeDTO;
import com.itheima.pinda.DTO.entity.pojo.base.PdGoodsType;
import com.itheima.pinda.DTO.entity.pojo.truck.PdTruckType;
import com.itheima.pinda.DTO.entity.pojo.truck.PdTruckTypeGoodsType;
import com.itheima.pinda.feign.service.common.PdGoodsTypeService;
import com.itheima.pinda.feign.service.truck.PdTruckTypeGoodsTypeService;
import com.itheima.pinda.feign.service.truck.PdTruckTypeService;
import com.itheima.pinda.mapper.truck.PdTruckTypeMapper;
import com.itheima.pinda.utils.CustomIdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车辆类型表 服务实现类
 * </p>
 *
 * @author xy
 * @since 2021-11-11
 */
@Service
@Transactional
public class PdTruckTypeServiceImpl extends ServiceImpl<PdTruckTypeMapper, PdTruckType> implements PdTruckTypeService {

    @Autowired
    private CustomIdGenerator customIdGenerator;
    @Autowired
    private PdTruckTypeGoodsTypeService truckTypeGoodsTypeService;
    @Autowired
    private PdGoodsTypeService goodsTypeService;

    /**
     * 查询所有的商品类型
     * @return List<PdTruckTypeDTO>
     */
    @Override
    public List<PdTruckTypeDTO> findTruckAll() {
        QueryWrapper<PdTruckType> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        List<PdTruckType> pdTruckTypes = super.baseMapper.selectList(wrapper);
        //        将适用的商品类型添加到PdTruckTypeDTO，
        List<PdTruckTypeDTO> typeDTOS = new ArrayList<>();
        for (PdTruckType truckType : pdTruckTypes
        ) {

            PdTruckTypeDTO dto = new PdTruckTypeDTO();
            BeanUtils.copyProperties(truckType, dto);



//            查询车辆类型对应适合的所有商品类型编号
            List<PdTruckTypeGoodsType> gid = truckTypeGoodsTypeService.seletGid(truckType.getId());


            List<String> list = new ArrayList<>();

//            查询所有对应的商品类型的名称，并添加到goodsName
            for (PdTruckTypeGoodsType type: gid
            ) {
                PdGoodsType goodsType = goodsTypeService.findById(type.getGoodsTypeId());
               list.add(goodsType.getId());
            }

//            将商品类型名称添加到车辆类型
            dto.setGoodsTypeId(list);
            typeDTOS.add(dto);
        }
        return typeDTOS;
    }

    /**
     * 重写高级查询的方法
     */
    @Override
    public List<PdTruckTypeDTO> findAdvance(String name, String allowableWeight, String allowableVolume) {

        //查询出条件下的车辆类型表
        QueryWrapper<PdTruckType> wrapper = new QueryWrapper<>();

        if (!ObjectUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!ObjectUtils.isEmpty(allowableWeight)){
            wrapper.like("allowable_load",allowableWeight);
        }
        if (!ObjectUtils.isEmpty(allowableVolume)){
            wrapper.like("allowable_volume",allowableVolume);
        }
        wrapper.eq("status",1);
        List<PdTruckType> list = super.baseMapper.selectList(wrapper);

//        System.out.println(list);


//        将适用的商品类型添加到PdTruckTypeDTO，
        List<PdTruckTypeDTO> typeDTOS = new ArrayList<>();
        for (PdTruckType truckType : list
        ) {

            PdTruckTypeDTO dto = new PdTruckTypeDTO();
            BeanUtils.copyProperties(truckType, dto);



//            查询车辆类型对应适合的所有商品类型编号
            List<PdTruckTypeGoodsType> gid = truckTypeGoodsTypeService.seletGid(truckType.getId());


            List<String> list1 = new ArrayList<>();

//            查询所有对应的商品类型的名称，并添加到goodsName
            for (PdTruckTypeGoodsType type: gid
            ) {
                PdGoodsType goodsType = goodsTypeService.findById(type.getGoodsTypeId());
                list1.add(goodsType.getId());
            }

//            将商品类型名称添加到车辆类型
            dto.setGoodsTypeId(list1);
            typeDTOS.add(dto);
        }
        return typeDTOS;
    }


    @Override
    public boolean addTruck(PdTruckTypeDTO dto) {

//        判断对象为空
        if (ObjectUtils.isEmpty(dto)) {
            throw new NullPointerException("传输的对象为空");
        }

//        提取出在dto对象的商品类型名
        List<String> typeIds = dto.getGoodsTypeId();


//      将dto对象复制到添加对象中，然后进行添加操作
        PdTruckType truckType = new PdTruckType();
        BeanUtils.copyProperties(dto,truckType);
//        id加密
        truckType.setId(customIdGenerator.nextId(truckType) + "");
        int insert = super.baseMapper.insert(truckType);

        int insert1 = 0;
        for (String id: typeIds
             ) {


//            将数据存储到中间表中
            PdTruckTypeGoodsType type = new PdTruckTypeGoodsType();
            type.setId(customIdGenerator.nextId(type)+"");
            type.setTruckTypeId(truckType.getId());
            type.setGoodsTypeId(id);


             insert1 = truckTypeGoodsTypeService.getBaseMapper().insert(type);
        }


        return insert != 0 && insert1 != 0;
    }

    @Override
    public boolean updateTruck(PdTruckTypeDTO dto,List<String > goodsTypeId) {
        if (ObjectUtils.isEmpty(dto)) {
            throw new NullPointerException("传输的对象为空");
        }


//      将dto对象复制到添加对象中，然后进行修改操作
        PdTruckType truckType = new PdTruckType();
        BeanUtils.copyProperties(dto,truckType);

        int insert = super.baseMapper.updateById(truckType);

//        删除中间表的相关数据
        QueryWrapper<PdTruckTypeGoodsType> wrapper = new QueryWrapper<>();
        wrapper.eq("truck_type_id",truckType.getId());
        truckTypeGoodsTypeService.getBaseMapper().delete(wrapper);

//        将几个商品类型名称转成集合

        int insert1 = 0;
        for (String id: goodsTypeId
        ) {

//            将数据存储到中间表中
            PdTruckTypeGoodsType type = new PdTruckTypeGoodsType();
            type.setId(customIdGenerator.nextId(type)+"");
            type.setTruckTypeId(truckType.getId());
            type.setGoodsTypeId(id);


            insert1 = truckTypeGoodsTypeService.getBaseMapper().insert(type);
        }


        return insert != 0 || insert1 != 0;
    }

    @Override
    public boolean deleteUpd(String id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new NullPointerException("你的编号为空");
        }
        PdTruckType truckType = super.baseMapper.selectById(id);
        truckType.setStatus(0);

        //        删除中间表的相关数据
        QueryWrapper<PdTruckTypeGoodsType> wrapper = new QueryWrapper<>();
        wrapper.eq("truck_type_id",truckType.getId());
        truckTypeGoodsTypeService.getBaseMapper().delete(wrapper);

        int insert = super.baseMapper.updateById(truckType);

        return insert != 0;
    }

    @Override
    public boolean deleteReal(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new NullPointerException("你的编号为空");
        }
        int insert = super.baseMapper.deleteById(id);

        return insert != 0;
    }
}
